package com.yami.shop.api.util;

public class Encryptor {
    public Encryptor() {
    }

    public static String decode(String pd) {
        return priDecode(pd);
    }

    public static String encrypt(String pd) {
        return priEncrypt(pd);
    }

    private static String priDecode(String pd) {
        if (pd != null && pd.length() <= 50) {
            char[] A1 = new char[50];
            char[] A3 = new char[50];
            char[] A2 = new char[50];
            int[] t2 = new int[]{5, 14, 12, 9, 11, 2, 0, 15, 7, 3, 4, 13, 1, 6, 10, 8};
            char[] t3 = new char[]{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
            int i = 0;
            int j = 0;
            int key = 32;
            A3 = pd.toCharArray();

            for (int k = pd.length(); i < k && A3[i] != 0; ++j) {
                A1[j] = A3[i];
                int p = 0;

                while (p < 16 && A1[j] != t3[p++]) {
                    ;
                }

                A2[j] = A3[i + 1];
                int q = 0;

                while (q < 16 && A2[j] != t3[q++]) {
                    ;
                }

                A1[j] = (char) (t2[p - 1] * 16 + t2[q - 1]);
                int temp = A1[j];
                temp = temp ^ key;
                A1[j] = (char) temp;
                i += 2;
            }

            A1[j] = 0;
            String value = String.valueOf(A1).trim();
            return value;
        } else {
            throw new IllegalArgumentException("参数不合法");
        }
    }

    private static String priEncrypt(String pd) {
        if (pd != null && pd.length() <= 24) {
            char[] A1 = new char[50];
            char[] A2 = new char[50];
            int[] t1 = new int[]{6, 12, 5, 9, 10, 0, 13, 8, 15, 3, 14, 4, 2, 11, 1, 7};
            char[] t3 = new char[]{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
            int i = 0;
            int j = 0;
            int key = 32;
            A1 = pd.toCharArray();

            for (int k = pd.length(); j < k; ++j) {
                byte temp = (byte) A1[j];
                temp ^= key;
                A1[j] = (char) temp;
                int x = temp / 16;
                int y = temp % 16;
                A2[i++] = t3[t1[x]];
                A2[i++] = t3[t1[y]];
            }

            A2[i] = 0;
            String value = String.valueOf(A2).trim();
            return value;
        } else {
            throw new IllegalArgumentException("参数不合法");
        }
    }
}
